﻿/*
 * 文件名称：栏目管理页面
 * 程序员：yhe
 * 创建时间：2009-09-11
 * 
 * 功能说明：以树形结构显示栏目管理,操作: 增,册,改,排序,该页面没有分页处理
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Whir_System_Module_Role_ColumnNewList : System.Web.UI.Page
{
    protected string colIDs = "";
    protected string DropdownList = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (Request.QueryString["ID"] != null)
        {
            string colID = Request.QueryString["ID"];

            colIDs = colID + "," + GetAllTypeID(colID).Trim(',').Trim(',');
        }

        CreateSelect();
        CreateTree();

        if(Request.QueryString["op"] != null)
        {
            //showhint
            string op = Request.QueryString["op"].ToString().ToLower();
            string msg = "";
            if (op == "")
            {
                return;
            }
            else if (op == "add")
            {
                msg = "增加栏目成功!";
            }
            else if (op == "modify")
            {
                msg = "修改栏目成功!";
            }
            else if (op == "delete")
            {
                msg = "删除栏目成功!";
            }
            string script = string.Format("<script>showhint('0','{0}')</script>", msg);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "init", script);
        }
    }

    /// <summary>
    /// 创建下拉列表框的选项
    /// </summary>
    private void CreateSelect()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<span id=\"{3}\">{2}&nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"ShowAndHidden('{1}','{5}');\">修改</a></span>");
        sb.Append("<span id=\"{4}\" style=\"display:none;\">");
        sb.Append("<select id=\"{0}\" style=\"height:22px; width:120px;\">");
        sb.Append("<option value=\"-1\">--请选择表单--</option>");
        sb = CreateOptgroup(sb);
        sb.Append("</select>");
        sb.Append("&nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"dropClick('{1}');\">保存</a>");
        sb.Append("&nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"Cancel('{1}');\">取消</a>");
        sb.Append("</span>");


        DropdownList = sb.ToString();
    }

    /// <summary>
    /// 创建下拉列表框的功能模块组
    /// </summary>
    private StringBuilder CreateOptgroup(StringBuilder sb)
    {
        string strSQL = String.Format("SELECT modelID, modelName FROM Whir_Model ORDER BY modelID");
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("<optgroup label=\"" + dt.Rows[i]["modelName"].ToString() + "\">");
            CreateSelectNode(sb, dt.Rows[i]["modelID"].ToString());
        }

        return sb;
    }

    /// <summary>
    /// 创建下拉列表框的表单节点
    /// </summary>
    private StringBuilder CreateSelectNode(StringBuilder sb, string modelID)
    {
        string strSQL = String.Format("SELECT modelFormID, formName FROM Whir_ModelForm WHERE modelID = {0}", modelID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("<option value=\"" + dt.Rows[i]["modelFormID"].ToString() + "\" style=\"color:#6E6E6E\">" + dt.Rows[i]["formName"].ToString() + "</option>");
        }

        return sb;
    }

    

    /// <summary>
    /// 创建一级栏目
    /// </summary>
    private void CreateTree()
    {
        string TreeTableString = "";

        string parentID = "0";

        string strSQL = string.Format("SELECT columnID,columnName,parentID,formID,(SELECT formName FROM Whir_ModelForm WHERE modelFormID  = Whir_Column.formID) formName FROM Whir_Column WHERE ParentID={0} Order by ParentID,sort", parentID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        for (int k = 0; k < dt.Rows.Count; k++)
        {
            string imgTr = "";
            if (dt.Rows.Count == 1) //只有一行数据，不进行操作
            {
                imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" /></td>";
            }
            else if (k == 0)
            {
                imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','down','" + dt.Rows[k + 1]["ColumnID"].ToString() + "')\" /></td>";
            }
            else if (k == dt.Rows.Count - 1)
            {
                imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','up','" + dt.Rows[k - 1]["ColumnID"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" disabled=\"disabled\" /></td>";
            }
            else
            {
                imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','up','" + dt.Rows[k - 1]["ColumnID"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','down','" + dt.Rows[k + 1]["ColumnID"].ToString() + "')\" /></td>";
            }

            TreeTableString += "<tr id=\"node-" + dt.Rows[k]["ColumnID"].ToString() + "\">";
            TreeTableString += "<td><span class=\"folder\" onclick='$(\"#node-" + dt.Rows[k]["ColumnID"].ToString() + "\").toggleBranch();'>" + dt.Rows[k]["ColumnName"].ToString() + "(" + dt.Rows[k]["columnID"] + ")</span></td>";
            //TreeTableString += "<td></td>";
            TreeTableString += "<td>" + String.Format(DropdownList, "select" + dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["formID"].ToString() == "-1" ? "" : dt.Rows[k]["formName"].ToString(), "show" + dt.Rows[k]["ColumnID"].ToString(), "hidden" + dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["formID"]) + "</td>";
            TreeTableString += imgTr;
            TreeTableString += "<td><a href=\"AddColumn.aspx?Action=Modify&ColumnID=" + dt.Rows[k]["ColumnID"].ToString() + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=550&modal=true\" class=\"thickbox\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:ConFirm('" + dt.Rows[k]["ColumnID"].ToString() + "')\">删除</a></td>";
            TreeTableString += "</tr>";

            TreeTableString += CreateSubTreeNode(dt.Rows[k]["ColumnID"].ToString());
        }

        Literal1.Text = TreeTableString;
    }


    /// <summary>
    /// 创建一级栏目外的子栏目
    /// </summary>
    /// <param name="columnID"></param>
    /// <returns></returns>
    private string CreateSubTreeNode(string columnID)
    {
        string SubNodeString = "";

        string strSQL = string.Format("SELECT columnID,columnName,parentID,formID,(SELECT formName FROM Whir_ModelForm WHERE modelFormID  = Whir_Column.formID) formName FROM Whir_Column WHERE ParentID={0} Order by ParentID,sort", columnID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        if (dt.Rows.Count > 0)
        {
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                string imgTr = "";
                if (dt.Rows.Count == 1) //只有一行数据，不进行操作
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" /></td>";
                }
                else if (k == 0)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up1.gif\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','down','" + dt.Rows[k + 1]["ColumnID"].ToString() + "')\" /></td>";
                }
                else if (k == dt.Rows.Count - 1)
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','up','" + dt.Rows[k - 1]["ColumnID"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down1.gif\" disabled=\"disabled\" /></td>";
                }
                else
                {
                    imgTr = "<td align=\"center\"><img src=\"../../image/ModelIcon/up.gif\" style='cursor:pointer;' title='向上移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','up','" + dt.Rows[k - 1]["ColumnID"].ToString() + "')\" />&nbsp;<img src=\"../../image/ModelIcon/down.gif\" style='cursor:pointer;' title='向下移' onclick=\"ChangeColumnSotr('" + dt.Rows[k]["ColumnID"].ToString() + "','down','" + dt.Rows[k + 1]["ColumnID"].ToString() + "')\" /></td>";
                }

                SubNodeString += "<tr id=\"node-" + dt.Rows[k]["ColumnID"].ToString() + "\" class=\"child-of-node-" + dt.Rows[k]["parentID"].ToString() + "\">";
                SubNodeString += "<td><span class=\"folder\" onclick='$(\"#node-" + dt.Rows[k]["ColumnID"].ToString() + "\").toggleBranch();'>" + dt.Rows[k]["ColumnName"].ToString() + "(" + dt.Rows[k]["columnID"] + ")</span></td>";
                SubNodeString += "<td>" + String.Format(DropdownList, "select" + dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["formID"].ToString() == "-1" ? "" : dt.Rows[k]["formName"].ToString(), "show" + dt.Rows[k]["ColumnID"].ToString(), "hidden" + dt.Rows[k]["ColumnID"].ToString(), dt.Rows[k]["formID"]) + "</td>";
                SubNodeString += imgTr;
                SubNodeString += "<td><a href=\"AddColumn.aspx?Action=Modify&ColumnID=" + dt.Rows[k]["ColumnID"].ToString() + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=550&modal=true\" class=\"thickbox\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:ConFirm('" + dt.Rows[k]["ColumnID"].ToString() + "')\">删除</a></td>";
                SubNodeString += "</tr>";
                SubNodeString += CreateSubTreeNode(dt.Rows[k]["ColumnID"].ToString());
            }
        }

        return SubNodeString;
    }


    private string GetAllTypeID(string colunmID)
    {
        string strResult = "";
        if (colunmID == "") return strResult;

        string strSQL = "SELECT parentID FROM Whir_Column WHERE parentID != 0 AND columnID = " + colunmID;
        object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSQL);

        try
        {
            if (obj != null)
            {
                strResult += obj.ToString() + ",";
                strResult += GetAllTypeID(obj.ToString());
            }
        }
        catch { }

        return strResult;
    }
}
